home *** CD-ROM | disk | FTP | other *** search
/ Get a Grip Tennis by Wilson / Get a Grip Tennis by Wilson.iso / pc / wilson.dxr / Internal_55_Constrain to Line.ls < prev    next >
Encoding:
Text File  |  2003-08-12  |  10.2 KB  |  219 lines

  1. property pSprite, pSpriteNum, pActive, pVector, pVectorLength, pDestination, pOrigin, pQTSpeed, pInited, pPosition, pDirection, pDistance, pPointH, pPointV, pBroadcast
  2.  
  3. on getBehaviorDescription me
  4.   return "CONSTRAIN TO LINE" & RETURN & RETURN & "[Advanced]" & RETURN & RETURN & "Creates a slider constrained to a straight line." & RETURN & RETURN & "Place the sprite at a position indicating the lowest value of the slider (typically the leftmost position) and use the behavior parameters to determine the direction the slider may be dragged." & RETURN & RETURN & "There are two methods to determine the direction of the slider:" & RETURN & RETURN & "The simplest is to select a direction (such as 'right') and a distance (in pixels) the slider can move. " & "Presets are available for each 45 degree angle." & RETURN & RETURN & "The other method is to choose 'point' and a coordinate for the sprite to move toward, allowing movements on other angles. " & "The initial position for the sprite to appear along the path is set by a number from 0 to 1 (default is 0.5, halfway between the maximum and minimum positions on the path)." & RETURN & RETURN & "The behavior broadcasts its current setting (always a number between 0 and 1) if the broadcast flag is set to anything but 0. " & "To intercept the broadcast, place an mConstrainedValue handler in a behavior on the target sprite." & RETURN & RETURN & "Example:" & RETURN & "  on mConstrainedValue(me, vSlider, vValue)" & RETURN & RETURN & "vSlider is the sprite number of the slider sending the message (enabling tests if there is more than one slider operating); vValue is the slider's current position." & RETURN & RETURN & "Use the mSetConstrainPos method to set the position of the slider from another handler. " & "For example," & RETURN & RETURN & "  sendSprite(sliderSpriteNum, #mSetConstrainPos, 0.66, FALSE)" & RETURN & RETURN & "would set the slider to a position two-thirds of the way between minimum and maximum positions without sending a new message to the broadcast sprite." & RETURN & RETURN & "The mIncrementConstrainPos handler can move the slider a set distance. " & "For example," & RETURN & RETURN & "  sendSprite(sliderSprite, #mIncrementConstrainPos, 0.05)" & RETURN & RETURN & "would move the sprite 1/20th of the distance from the minimum position to the maximum position. " & "Negative values move toward the minimum position." & RETURN & RETURN & "PERMITTED MEMBER TYPES:" & RETURN & "bitmap, Flash, QuickTime" & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Position slider first appears on path" & RETURN & "* Direction sprite will slide" & RETURN & "* Distance sprite moves (used for relative direction)" & RETURN & "* Vertical point coordinate (used for specific point)" & RETURN & "* Horizontal point coordinate (used for specific point)" & RETURN & "* Broadcast flag for slider position"
  5. end
  6.  
  7. on getBehaviorTooltip me
  8.   return "[ADVANCED]" & RETURN & RETURN & "Makes a sprite draggable along a straight line." & RETURN & RETURN & "Place a sprite at one end of the line it should be dragged along, set parameters to determine the other end of the line, and where the draggable sprite should first appear. " & "The behavior can report its position along the line as a message to another sprite."
  9. end
  10.  
  11. on beginSprite me
  12.   mInitialize(me)
  13. end
  14.  
  15. on prepareFrame me
  16.   mUpdate(me)
  17. end
  18.  
  19. on mouseDown me
  20.   pActive = 1
  21.   if sprite(pBroadcast).member.type = #quickTimeMedia then
  22.     pQTSpeed = sprite(pBroadcast).movieRate
  23.     sprite(pBroadcast).movieRate = 0
  24.   end if
  25. end
  26.  
  27. on relocScrub me, where
  28.   case where of
  29.     #pu:
  30.       newLoc = point(278, 377)
  31.     #int:
  32.       newLoc = point(124, 377)
  33.     #mm:
  34.       newLoc = point(386, 457)
  35.     #offs:
  36.       newLoc = point(-1000, -1000)
  37.   end case
  38.   updateStage()
  39.   sprite(pBroadcast).mInitialize()
  40.   mInitialize2(me, newLoc)
  41. end
  42.  
  43. on mInitialize2 me, newLoc
  44.   pSpriteNum = me.spriteNum
  45.   pSprite = sprite(pSpriteNum)
  46.   pActive = 0
  47.   pOrigin = newLoc
  48.   pSprite.loc = newLoc
  49.   vStage = (the stage).rect
  50.   vDiagonal = integer(pDistance * sqrt(2.0) / 2)
  51.   case pDirection of
  52.     "Point":
  53.       pDestination = point(pPointH, pPointV)
  54.     "Up":
  55.       pDestination = point(pSprite.locH, max(0, pSprite.locV - pDistance))
  56.     "Down":
  57.       pDestination = point(pSprite.locH, min(vStage.height, pSprite.locV + pDistance))
  58.     "Left":
  59.       pDestination = point(max(0, pSprite.locH - pDistance), pSprite.locV)
  60.     "Right":
  61.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), pSprite.locV)
  62.     "Up Left":
  63.       pDestination = point(max(0, pSprite.locH - vDiagonal), max(0, pSprite.locV - vDiagonal))
  64.     "Up Right":
  65.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), max(0, pSprite.locV - vDiagonal))
  66.     "Down Left":
  67.       pDestination = point(max(0, pSprite.locH - vDiagonal), min(vStage.width, pSprite.locV + vDiagonal))
  68.     "Down Right":
  69.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), min(vStage.width, pSprite.locV + vDiagonal))
  70.   end case
  71.   pVector = pDestination - pOrigin
  72.   pVectorLength = mVectorLength(pVector)
  73.   if sprite(pBroadcast).member.type = #quickTimeMedia then
  74.     pQTSpeed = sprite(pBroadcast).movieRate
  75.   else
  76.     pQTSpeed = 0
  77.   end if
  78.   vMember = pSprite.member
  79.   case vMember.type of
  80.     #animGif, #flash, #quickTimeMedia, #digitalVideo, #vectorShape:
  81.       if vMember.directToStage then
  82.         alert("Sprite" && pSpriteNum & ": Direct To Stage media may cause" && "playback problems with the 'Constrain to Line' behavior.")
  83.       end if
  84.   end case
  85.   mSetConstrainPos(me, pPosition, 1)
  86.   pInited = 0
  87. end
  88.  
  89. on mInitialize me
  90.   pSpriteNum = me.spriteNum
  91.   pSprite = sprite(pSpriteNum)
  92.   pActive = 0
  93.   pOrigin = pSprite.loc
  94.   vStage = (the stage).rect
  95.   vDiagonal = integer(pDistance * sqrt(2.0) / 2)
  96.   case pDirection of
  97.     "Point":
  98.       pDestination = point(pPointH, pPointV)
  99.     "Up":
  100.       pDestination = point(pSprite.locH, max(0, pSprite.locV - pDistance))
  101.     "Down":
  102.       pDestination = point(pSprite.locH, min(vStage.height, pSprite.locV + pDistance))
  103.     "Left":
  104.       pDestination = point(max(0, pSprite.locH - pDistance), pSprite.locV)
  105.     "Right":
  106.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), pSprite.locV)
  107.     "Up Left":
  108.       pDestination = point(max(0, pSprite.locH - vDiagonal), max(0, pSprite.locV - vDiagonal))
  109.     "Up Right":
  110.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), max(0, pSprite.locV - vDiagonal))
  111.     "Down Left":
  112.       pDestination = point(max(0, pSprite.locH - vDiagonal), min(vStage.width, pSprite.locV + vDiagonal))
  113.     "Down Right":
  114.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), min(vStage.width, pSprite.locV + vDiagonal))
  115.   end case
  116.   pVector = pDestination - pOrigin
  117.   pVectorLength = mVectorLength(pVector)
  118.   if sprite(pBroadcast).member.type = #quickTimeMedia then
  119.     pQTSpeed = sprite(pBroadcast).movieRate
  120.   else
  121.     pQTSpeed = 0
  122.   end if
  123.   vMember = pSprite.member
  124.   case vMember.type of
  125.     #animGif, #flash, #quickTimeMedia, #digitalVideo, #vectorShape:
  126.       if vMember.directToStage then
  127.         alert("Sprite" && pSpriteNum & ": Direct To Stage media may cause" && "playback problems with the 'Constrain to Line' behavior.")
  128.       end if
  129.   end case
  130.   mSetConstrainPos(me, pPosition, 1)
  131.   pInited = 0
  132. end
  133.  
  134. on mUpdate me
  135.   if not pInited then
  136.     mSetConstrainPos(me, pPosition, 1)
  137.     pInited = 1
  138.   end if
  139.   if pActive then
  140.     if the mouseUp then
  141.       pActive = 0
  142.       if sprite(pBroadcast).member.type = #quickTimeMedia then
  143.         sprite(pBroadcast).movieRate = pQTSpeed
  144.       end if
  145.     else
  146.       vDiff = pSprite.loc - the mouseLoc
  147.       vDist = mVectorLength(vDiff)
  148.       if vDist > 0 then
  149.         vPercent = min(1.0, vDist / pVectorLength) / 2
  150.         vAhead = min(pPosition + vPercent, 1.0)
  151.         vBehind = max(pPosition - vPercent, 0.0)
  152.         vAheadPoint = pOrigin + mScaleVector(pVector, vAhead)
  153.         vBehindPoint = pOrigin + mScaleVector(pVector, vBehind)
  154.         vAheadDist = mVectorLength(vAheadPoint - the mouseLoc)
  155.         vBehindDist = mVectorLength(vBehindPoint - the mouseLoc)
  156.         vCompare = vAheadDist - vBehindDist
  157.         if vCompare <> 0.0 then
  158.           if vCompare < 0 then
  159.             if vDist > vAheadDist then
  160.               mSetConstrainPos(me, vAhead, 1)
  161.             end if
  162.           else
  163.             if vDist > vBehindDist then
  164.               mSetConstrainPos(me, vBehind, 1)
  165.             end if
  166.           end if
  167.         end if
  168.       end if
  169.     end if
  170.   end if
  171. end
  172.  
  173. on mScaleVector vVector, vScale
  174.   return vVector * integer(1000 * vScale) / 1000
  175. end
  176.  
  177. on mVectorLength vVector
  178.   vSquare = (vVector.locH * vVector.locH) + (vVector.locV * vVector.locV)
  179.   return sqrt(float(vSquare))
  180. end
  181.  
  182. on mIncrementConstrainPos me, vIncrement
  183.   mSetConstrainPos(me, pPosition + vIncrement, 1)
  184. end
  185.  
  186. on mSetConstrainPos me, vPosition, vUpdate
  187.   pPosition = max(min(1.0, vPosition), 0.0)
  188.   pSprite.loc = pOrigin + mScaleVector(pVector, pPosition)
  189.   if pBroadcast then
  190.     if vUpdate then
  191.       sendSprite(pBroadcast, #mConstrainedValue, pSpriteNum, pPosition)
  192.     end if
  193.   end if
  194. end
  195.  
  196. on isOKToAttach me, aSpriteType, aSpriteNum
  197.   return aSpriteType = #graphic
  198. end
  199.  
  200. on getPropertyDescriptionList me
  201.   if not (the currentSpriteNum) then
  202.     exit
  203.   end if
  204.   vStage = (the stage).rect
  205.   vStageMeasure = max(vStage.height, vStage.width)
  206.   vPDList = [:]
  207.   setaProp(vPDList, #pDirection, [#comment: "Constraint direction (relative to current position)", #format: #string, #default: "Right", #range: mDirList()])
  208.   setaProp(vPDList, #pDistance, [#comment: "Distance (in pixels)", #format: #integer, #default: min(100, vStageMeasure), #range: [#min: 10, #max: vStageMeasure]])
  209.   setaProp(vPDList, #pPosition, [#comment: "Initial position on line (from 0 to 1)", #format: #float, #default: 0.5, #range: [#min: 0.0, #max: 1.0]])
  210.   setaProp(vPDList, #pPointH, [#comment: "Point Horizontal Value", #format: #integer, #default: vStage.width / 2, #range: [#min: 0, #max: vStage.width]])
  211.   setaProp(vPDList, #pPointV, [#comment: "Point Vertical Value", #format: #integer, #default: vStage.height / 2, #range: [#min: 0, #max: vStage.height]])
  212.   setaProp(vPDList, #pBroadcast, [#comment: "Broadcast position to sprite (0 = no broadcast)", #format: #integer, #default: 0, #range: [#min: 0, #max: the lastChannel]])
  213.   return vPDList
  214. end
  215.  
  216. on mDirList
  217.   return ["Point", "Up", "Down", "Left", "Right", "Up Left", "Up Right", "Down Left", "Down Right"]
  218. end
  219.